﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Networking.Connectivity;
using Windows.System;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Web.Syndication;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace AssoZone
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class MainPage : AssoZone.Common.LayoutAwarePage
    {
        
       

        ObservableCollection<Feed> Feeds { get; set; }
        public MainPage()
        {

            //((App)Application.Current).Urls.Add("");


            this.InitializeComponent();
            Feeds = new ObservableCollection<Feed>();
            if (NetworkInformation.GetInternetConnectionProfile().GetNetworkConnectivityLevel() ==
                NetworkConnectivityLevel.InternetAccess)
            {
               
            }
            else
            {
                ShowMessage("No internet", " No Internet access :(");
            }
            MyGridView.ItemsSource = Feeds;
        }
        public async Task ShowMessage(string title, string message)
        {
            await new MessageDialog(message, title).ShowAsync();
            Application.Current.Exit();
        }

        public async Task LoadFeeds(string url)
        {
            var client = new SyndicationClient();
            var feeds = await client.RetrieveFeedAsync(new Uri(url));
            foreach (var feed in feeds.Items)
            {
                feed.ToString();
                var feedToAdd = new Feed()
                {
                    Description = feed.Summary.Text,
                    Title = feed.Title.Text,
                    Url = feed.Id,
                    ImageUrl = feed.Links.Where(link => link.MediaType.Equals("image/jpeg"))
                    .Select(l => l.Uri.ToString())
                    .FirstOrDefault()
                };
                Feeds.Add(feedToAdd);
                
            }

        }





        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            Feeds.Clear();
            foreach (var url in ((App)Application.Current).Urls)
            {
                LoadFeeds(url.FluxAsso);

            }

            MyGridView2.ItemsSource = ((App)Application.Current).Urls;
        }

        private void MyGridView_OnItemClick(object sender, ItemClickEventArgs e)
        {
            var feedClicked = (Feed)e.ClickedItem;
            if (!string.IsNullOrEmpty(feedClicked.Url))
            {
                Launcher.LaunchUriAsync(new Uri(feedClicked.Url));
            }
        }

        private void Ajout_Asso(object sender, RoutedEventArgs e)
        {
            if (this.Frame != null)
            {
                this.Frame.Navigate(typeof(AjouterAsso2),1);
            }

        }



    }
    public class Feed
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public string ImageUrl { get; set; }
        public string Url { get; set; }
    }

    public class Association
    {
        public string Name { get; set; }
        public string ImageAsso { get; set; }
        public string FluxAsso { get; set; }
        public string EMail { get; set; }
        public string Password { get; set; }
    }
}
